package com.dp.client.views.almacenes;

import java.util.Date;

import com.dp.client.dto.PaqueteDTO;
import com.dp.client.util.Alerts;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.CaptionPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.IntegerBox;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.ClickEvent;

public class RecogerDialog extends PopupPanel {
	private Label lblNewLabel;
	private CaptionPanel cptnpnlVerificarReceptor;
	private Button btnCerrar;
	private Button btnVerificar;
	private IntegerBox txtCodigo;
	private FlexTable flexTable;
	private PaqueteDTO paquete;
	private TablaPaquetesAlmacen padre;

	public RecogerDialog(PaqueteDTO object, TablaPaquetesAlmacen tablaPaquetesAlmacen) {
		super(true);
		this.paquete=object;
		this.padre=tablaPaquetesAlmacen;
		cptnpnlVerificarReceptor = new CaptionPanel("Verificar receptor");
		setWidget(cptnpnlVerificarReceptor);
		cptnpnlVerificarReceptor.setSize("auto", "auto");
		
		flexTable = new FlexTable();
		cptnpnlVerificarReceptor.setContentWidget(flexTable);
		flexTable.setSize("5cm", "3cm");
		
		lblNewLabel = new Label("Ingrese codigo :");
		flexTable.setWidget(0, 0, lblNewLabel);
		
		txtCodigo = new IntegerBox();
		flexTable.setWidget(1, 0, txtCodigo);
		
		HorizontalPanel horizontalPanel = new HorizontalPanel();
		flexTable.setWidget(2, 0, horizontalPanel);
		
		btnVerificar = new Button("Verificar");
		btnVerificar.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				verificar();
			}
		});
		horizontalPanel.add(btnVerificar);
		
		btnCerrar = new Button("Cerrar");
		btnCerrar.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				cerrar();
			}
		});
		horizontalPanel.add(btnCerrar);
	}

	protected void verificar() {
		int codigo=Integer.parseInt(txtCodigo.getText());
		if (codigo == paquete.nroSecretoDestinatario){
			paquete.setFechaRecojo(new Date());
			paquete.setEstadoPaquete(4);
			padre.actualizar(paquete);
			Alerts.crearAlerta("El codigo es correcto");
			this.hide();
		}
		else
			Alerts.crearAlerta("El codigo no coincide");
	}

	protected void cerrar() {
		this.hide();
	}

}
